package com.by.config;

import com.by.bean.Goods;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.data.redis.core.script.RedisScript;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@Component
public class RedisUtils {

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @Autowired
    RedisTemplate<String, List<Goods>> redisTemplate;

    public void set(String key,String value){
        stringRedisTemplate.opsForValue().set(key,value);
    }

    public void set(String key,String value,long time){
        stringRedisTemplate.opsForValue().set(key,value,time, TimeUnit.SECONDS);
    }

    public void incr(String key,long num){
        stringRedisTemplate.opsForValue().increment(key,num);
    }

    public String get(String key){
        return stringRedisTemplate.opsForValue().get(key);
    }

    public Boolean setNx(String key,String value){
        return stringRedisTemplate.opsForValue().setIfAbsent(key,value);
    }

    public Boolean setNxEx(String key,String value,long time){
        return stringRedisTemplate.opsForValue().setIfAbsent(key,value,time,TimeUnit.SECONDS);
    }

    public Boolean delete(String key){
        return stringRedisTemplate.delete(key);
    }


    public Boolean setXx(String key,String value){
        return stringRedisTemplate.opsForValue().setIfPresent(key,value);
    }

    public long lpush(String key,String value){
        return stringRedisTemplate.opsForList().leftPush(key,value);
    }

    public String rpop(String key){
        return stringRedisTemplate.opsForList().rightPop(key);
    }

    public void hset(String key,String field,String value){
        HashOperations<String,String,String> hashOperations = stringRedisTemplate.opsForHash();
        hashOperations.put(key,field,value);
    }

    public void zadd(String key,String value,double score){
        ZSetOperations<String, String> zSetOperations = stringRedisTemplate.opsForZSet();
        zSetOperations.add(key,value,score);
    }

    public Set sortByDesc(String key, double min, double max){
        ZSetOperations<String, String> zSetOperations = stringRedisTemplate.opsForZSet();
        Set set = zSetOperations.reverseRangeByScore(key,min,max);
        return set;
    }

    public void setGoods(String key, List<Goods> goodsList,long timeout){
        redisTemplate.opsForValue().set(key,goodsList,timeout,TimeUnit.SECONDS);
    }

    public List<Goods> getGoods(String key){
        return (List<Goods>) redisTemplate.opsForValue().get(key);

    }

    /**
     *  执行lua脚本
     * @param lua
     * @param key
     * @return
     */
    public long execLua(String lua,List<String> key){
        RedisScript<Long> redisScript = RedisScript.of(lua,Long.class);
        return stringRedisTemplate.execute(redisScript,key);
    }


}
